/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import config.Debug;
import config.Settings;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class HttpRequest {

	public static void getWeather() throws MalformedURLException {
		HttpPostConnector c = new HttpPostConnector(new URL("http://localhost/player/weather.php"), null);
		c.post();
		// System.out.println(c.getResult());
		String[] array = c.getResult().split(",");
		// System.out.println(Arrays.toString(array));
		// System.out.println(array.length);
		// can test the result here
	}

	public static void getTime() {
	}

	public static void getNewestPlayerVersion() {
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken()
					+ "&reqtype=checkNewPlayer&version=" + Settings.VERSION);
			c.post();
			// double version = Double.parseDouble(c.getResult());
			// return version;
		} catch (MalformedURLException ex) {
			// this should never happen because the url passed in is guaranteed
			// to be valid
			Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
		} catch (NumberFormatException ex) {
			Debug.log("Player version received from server cannot be parsed as a floating point number", Debug.ERROR_LEVEL);
		}
		// return 7.0;
	}

	public static void postUpdateComplete() {
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken() + "&reqtype=completeupdate");
			c.post();
			// Debug.log("Sent update complete signal to server",
			// Debug.SYSTEM_LEVEL);
			Debug.msg("Sent update complete signal to server");
		} catch (MalformedURLException ex) {
			// this should never happen because the url passed in is guaranteed
			// to be valid
			Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void postLog(String logJson) {
		// logJson = URLEncoder.encode(logJson);
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken() + "&reqtype=sendLog" + "&log="
					+ logJson);
			c.post();
		} catch (MalformedURLException ex) {
			Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void postRecordId(int id) {
		// logJson = URLEncoder.encode(logJson);
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken() + "&reqtype=current_record"
					+ "&record_id=" + id);
			c.post();
		} catch (MalformedURLException ex) {
			Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void uploadLogFiles() {
		File logDirectory = new File(Settings.getLogsDirectory());
		if (logDirectory.exists() && logDirectory.isDirectory()) {
			for (File logFile : logDirectory.listFiles()) {
				System.out.println("Uploading " + logFile);
				try {
					InputStream serverInput = ClientHttpRequest.post(new URL("http://www.digitalmarketingbox.com/"),
							new Object[] { logFile.getName(), logFile });
				} catch (IOException ex) {
					Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
		}
	}

	public static void resendContent() {
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken() + "&reqtype=resendcontent");
			c.post();
		} catch (MalformedURLException ex) {
			// this should never happen because the url passed in is guaranteed
			// to be valid
			Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void main(String[] args) {
		resendContent();
	}
}
